package com.app.business.web.uni;

import com.alibaba.fastjson.JSON;
import com.app.business.model.CarLot;
import com.app.business.model.Contract;
import com.app.business.model.House;
import com.app.business.model.StorageRoom;
import com.app.business.service.uni.AppContractService;
import com.app.business.service.uni.AppHouseService;
import com.app.system.jpa.PageBean;
import com.app.system.permission.model.DataDict;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.dataType.DateUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.page.UViewUiUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by ddz on 2021/11/18.
 */
@RestController
@SuppressWarnings("all")
@RequestMapping("/uniApp/statistics/*")
public class UniAppStatisticsController {
    @Autowired
    private DataDictService dataDictService;
    @Autowired
    private AppHouseService appHouseService;
    @Autowired
    private AppContractService appContractService;


    /**
     * 选房统计
     *
     * @return
     */
    @RequestMapping("houseStatisticsDataJson")
    public Map houseStatisticsDataJson() {
        //通过数据字典配置得到相应展示信息
        Map phoneInfoMap = dataDictService.getMapByParentNameForName_Value("手机端公示信息配置");
        String zsProjectIdStr = phoneInfoMap.get("征收项目ID").toString();//征收项目id list
        String azProjectIdStr = phoneInfoMap.get("安置项目ID").toString();//安置项目id list

        //获取当前征收项目所分配房源
//        List<Integer> houseIdList = this.appHouseService.getHouseIdList(zsProjectIdStr);

        Map resultMap = Maps.newLinkedHashMap();
        //房源饼状图
        Map houseMap = this.appHouseService.getHouseStatisticsByAzProjectId(azProjectIdStr);
        resultMap.put("houseAllNum", houseMap.get("houseAllNum"));
        resultMap.put("houseAllArea", houseMap.get("houseAllArea"));
        resultMap.put("chooseNum", houseMap.get("chooseNum"));
        resultMap.put("chooseArea", houseMap.get("chooseArea"));
        resultMap.put("unChooseNum", houseMap.get("unChooseNum"));
        resultMap.put("unChooseArea", houseMap.get("unChooseArea"));
        //车位饼状图
        Map carLotMap = this.appHouseService.getCarLotStatisticsByAzProjectId(azProjectIdStr);
        resultMap.put("choosePtCarLotNum", carLotMap.get("choosePtCarLotNum"));
        resultMap.put("chooseRfCarLotNum", carLotMap.get("chooseRfCarLotNum"));
        resultMap.put("unChoosePtCarLotNum", carLotMap.get("unChoosePtCarLotNum"));
        resultMap.put("unChooseRfCarLotNum", carLotMap.get("unChooseRfCarLotNum"));


        //户型柱状图
        String houseHoldNumStr = "[";
        String allHouseNumStr = "[";
        String chooseNumStr = "[";
        String unChooseNumStr = "[";
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");
        Map<String, Map> houseHoldCountMap = this.appHouseService.getHouseHoldStatisticsByAzProjectId(azProjectIdStr);
        Map itemMap;
        if (houseHoldCountMap != null) {
            for (String dataDict : houseHoldCountMap.keySet()) {
                String hosueTypeName = houseHoldMap.get(dataDict) != null ? houseHoldMap.get(dataDict).getName() : "";
                houseHoldNumStr += "'" + hosueTypeName + "',";
                allHouseNumStr += "'" + houseHoldCountMap.get(dataDict).get("houseNum") + "',";
                chooseNumStr += "'" + houseHoldCountMap.get(dataDict).get("chooseNum") + "',";
            }
            if (houseHoldNumStr.length() > 1) {
                houseHoldNumStr = houseHoldNumStr.substring(0, houseHoldNumStr.length() - 1);
                allHouseNumStr = allHouseNumStr.substring(0, allHouseNumStr.length() - 1);
                chooseNumStr = chooseNumStr.substring(0, chooseNumStr.length() - 1);
            }
            houseHoldNumStr = houseHoldNumStr + "]";
            allHouseNumStr = allHouseNumStr + "]";
            chooseNumStr = chooseNumStr + "]";
        }
        resultMap.put("houseHoldNumStr", JSON.parse(houseHoldNumStr.toString()));
        resultMap.put("allHouseNumStr", JSON.parse(allHouseNumStr.toString()));
        resultMap.put("chooseNumStr", JSON.parse(chooseNumStr.toString()));

        Map<String, Map> buildAndTypeSumMap = this.appHouseService.getHouseHoldMap(azProjectIdStr);

        resultMap.put("buildAndTypeSumMap", buildAndTypeSumMap);
        Map<String, String> houseHoldStrMap = this.dataDictService.getMapByParentNameForValue_Name("房屋户型");

        resultMap.put("houseHoldStrMap", houseHoldStrMap);

        return resultMap;
    }


    /**
     * 已选房查询列表
     *
     * @param request
     * @return
     */
    @RequestMapping("choosedContractList")
    public Map choosedContractList(HttpServletRequest request) {
        //通过数据字典配置得到相应展示信息
        Map phoneInfoMap = dataDictService.getMapByParentNameForName_Value("手机端公示信息配置");
        String zsProjectIdStr = phoneInfoMap.get("征收项目ID").toString();//征收项目id list
        String azProjectIdStr = phoneInfoMap.get("安置项目ID").toString();//安置项目id list
        List<Integer> houseIdList = this.appHouseService.getHouseIdList(zsProjectIdStr);

        List<Map> listData = Lists.newArrayList();
        PageBean<Contract> contractPageBean = this.appContractService.getContractList(request, zsProjectIdStr);
        Map<String, List<House>> houseMap = this.appHouseService.getHouseByContractIdMap(houseIdList, azProjectIdStr);
        Map<String, List<CarLot>> carLotMap = this.appHouseService.getCarLotByContractIdMap(azProjectIdStr);
        Map<String, List<StorageRoom>> storageRoomMap = this.appHouseService.getStorageRoomByContractIdMap(azProjectIdStr);

        List<Contract> contractList = contractPageBean.getDatas();
        if (CollectionUtils.isNotEmpty(contractList)) {
            String houseStr = "";
            String carLotStr = "";
            String storageRoomStr = "";
            for (Contract contract : contractList) {
                houseStr = "";
                carLotStr = "";
                storageRoomStr = "";
                Map<String, Object> item = Maps.newHashMap();
                item.put("id", contract.getId());
                item.put("zsProjectName", StringUtils.getNotNull(contract.getZsProject().getProjectName()));
                item.put("name", StringUtils.getNotNull(contract.getName()));
                item.put("chooseHouseNum", StringUtils.getNotNull(contract.getChooseHouseNum()));
                item.put("chooseHouseDate", DateUtils.dateToStringFormat(contract.getChooseHouseDate(), "yyyy年MM月dd日 hh时MM分ss秒"));

                List<House> houseList = houseMap.get(contract.getId().toString());
                if (CollectionUtils.isNotEmpty(houseList)) {
                    houseStr = "房源：";
                    for (House house : houseList) {
                        houseStr = houseStr + house.getHouseNumStr() + "、";
                    }
                    houseStr = houseStr.substring(0, houseStr.length() - 1);
                }
                item.put("houseStr", StringUtils.getNotNull(houseStr));

                //车位明细
                List<CarLot> carLotList = carLotMap.get(contract.getId().toString());
                if (CollectionUtils.isNotEmpty(carLotList)) {
                    carLotStr = "车位：";
                    for (CarLot carLot : carLotList) {
                        carLotStr = carLotStr + carLot.getHouseNumStr() + "、";
                    }
                    carLotStr = carLotStr.substring(0, carLotStr.length() - 1);

                }
                item.put("carLotStr", StringUtils.getNotNull(carLotStr));

                //储物间明细
                List<StorageRoom> storageRoomList = storageRoomMap.get(contract.getId().toString());
                if (CollectionUtils.isNotEmpty(storageRoomList)) {
                    storageRoomStr = "储藏间：";
                    for (StorageRoom storageRoom : storageRoomList) {
                        storageRoomStr = storageRoomStr + storageRoom.getHouseNumStr() + "、";
                    }
                    storageRoomStr = storageRoomStr.substring(0, storageRoomStr.length() - 1);
                }
                item.put("storageRoomStr", StringUtils.getNotNull(storageRoomStr));

                listData.add(item);
            }
        }
        return UViewUiUtils.page(contractPageBean, listData);
    }


}

